package models

import "gorm.io/gorm"

type Book struct {
	gorm.Model
	Title      string
	Class      string //公司信息，资讯文章
	Author     string
	ReadNum    int
	CommentNum int
	Type       string //待发布，已发布，已下线
	Data       string
}

func (b *Book) GetBookByTitle() error {
	return DB.Where("title = ?", b.Title).Limit(1).Find(&b).Error
}
func (b *Book) DeleteBookByID() error {
	return DB.Delete(&b).Error
}
func (b *Book) CreateBook() error {
	return DB.Create(&b).Error
}
func (b *Book) GetBookList(title, class, t string, page int) (BookList []Book, err error) {
	sel := DB.Limit(5).Offset((page - 1) * 5)
	if title != "" {
		sel = sel.Where("title like ?", "%"+title+"%")
	}
	if class != "" {
		sel = sel.Where("class = ?", class)
	}
	if t != "" {
		sel = sel.Where("type = ?", t)
	}
	err = sel.Find(&BookList).Error
	return BookList, err
}
func (b *Book) UpdatesBook() error {
	return DB.Updates(&b).Error
}
